package com.dongdongshop.convertor;

import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期转换器
 */
public class DateConvertor implements Converter<String, Date> {

    @Override
    public Date convert(String s) {
        SimpleDateFormat sdf10 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf19 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            if(s != null && !"".equals(s)){
                if(s.length() == 10){
                    date = sdf10.parse(s);
                }else if(s.length() == 19){
                    date = sdf19.parse(s);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
